<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
abstract class LM_Ranking
{
    protected $_project = null;
    protected $_teams = array();
    protected $_matches = array();
    protected $_params = array();
    protected $_ranking = array();

    public function setProject($project)
    {
        $this->_project = $project;
    }

    public function setParam($key, $value)
    {
        $this->_params[$key] = $value;
    }

    public function getParam($key, $default = 0)
    {
        if (isset($this->_params[$key]))
            return $this->_params[$key];
        else
            return $default;
    }

    protected function setTeams()
    {
        $this->_teams = $this->_project->getTeams();
    }

    protected function setMatches()
    {
        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($this->_project->getID());
        $matchtable->addFilter("m.published = 1");

        $round = $this->getParam("round", 0);
        if ($round) {
            $matchtable->addFilter("m.match_round <= $round");
        }

        $this->_matches = $matchtable->getRows();
    }

    protected function setRanking()
    {

        $data = array();

        foreach ($this->_teams as $team) {
            $row = new LM_Ranking_Row($team);
            $data[$team->team_id] = $row;
        }

        $mode = $this->getParam("mode", 0);

        foreach ($this->_matches as $match) {

            $mrt = ($this->_project->getParam("allow_extra_time", 0)) ? $match->match_result_type : 0;

            $config = array();

            switch ($mrt) {
                case 1:
                    $config["points_for_victory"] = $this->_project->getParam("points_after_extra_time_v", 3);
                    $config["points_for_tie"] = $this->_project->getParam("points_after_extra_time_v", 1);
                    $config["points_for_defeat"] = $this->_project->getParam("points_after_extra_time_v", 0);
                    break;

                case 2:
                    $config["points_for_victory"] = $this->_project->getParam("points_after_penalty_v", 3);
                    $config["points_for_tie"] = $this->_project->getParam("points_after_penalty_t", 1);
                    $config["points_for_defeat"] = $this->_project->getParam("points_after_penalty_d", 1);
                    break;

                default:
                    $config["points_for_victory"] = $this->_project->getParam("points_after_regular_time_v", 3);
                    $config["points_for_tie"] = $this->_project->getParam("points_after_regular_time_t", 1);
                    $config["points_for_defeat"] = $this->_project->getParam("points_after_regular_time_d", 0);
                    break;
            }

            if ($mode == 0 || $mode == 1) {
                $m1 = $match->team1_id; //Key
                $e1 = $match->match_result_0_1; //Tore
                $e2 = $match->match_result_0_2; //Gegentore

                $row = $data[$m1];
                $row->addResult($e1, $e2, $config);
            }

            if ($mode == 0 || $mode == 2) {
                $m2 = $match->team2_id; //Key
                $e1 = $match->match_result_0_2; //Tore
                $e2 = $match->match_result_0_1; //Gegentore

                $row = $data[$m2];
                $row->addResult($e1, $e2, $config);
            }
        }

        uasort($data, array($this, "compare"));

        $this->_ranking = $data;
    }

    public function build()
    {
        $this->setTeams();
        $this->setMatches();
        $this->setRanking();
    }

    public function getRanking()
    {
        $this->build();
        return $this->_ranking;
    }

    abstract protected function compare(LM_Ranking_Row $a, LM_Ranking_Row $b);
}